package com.it.flowable.controller;

import com.it.flowable.dto.ApprovalVo;
import com.it.flowable.dto.ProcessModelVo;
import com.it.flowable.dto.TaskInfoVo;
import com.it.flowable.dto.TaskQo;
import com.it.flowable.model.FourLevelModel;
import com.it.flowable.model.GatewayModel;
import com.it.flowable.model.LeaveProcessModel;
import com.it.flowable.model.TestUserTaskModel;
import com.it.flowable.util.PrintModelUtils;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.TaskService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.it.flowable.util.PrintModelUtils.generateBpmnFile;

@Slf4j
@SpringBootTest
public class TestFlowableLeaveProcess {
    public static final String APPROVE_USER = "admin";

    public static final String LEAVE_PROCESS = "leaveProcess";

    public static final String LEADER_ZHANG = "leader_zhang";

    public static final String HR_WANG = "hr_wang";

    public static final String CEO_LI = "ceo_li";

    @Resource
    private ProcessModelController processModelController;
    @Resource
    private ProcessExecController processExecController;

    public static BpmnModel model = LeaveProcessModel.create();

    @Resource
    private TaskService taskService;

    @Test
    public void testModel2Xml() {
        generateBpmnFile(LeaveProcessModel.create(), LeaveProcessModel.PROCESS_KEY);
        generateBpmnFile(FourLevelModel.create(), FourLevelModel.PROCESS_KEY);
        generateBpmnFile(GatewayModel.create(), GatewayModel.PROCESS_KEY);
        generateBpmnFile(TestUserTaskModel.create(), TestUserTaskModel.PROCESS_KEY);
    }

    @Test
    public void testDeploy() {
        ProcessModelVo processModelVo = new ProcessModelVo("请假", LEAVE_PROCESS, PrintModelUtils.modelToXml(model));
        processModelController.deployProcess(processModelVo);
    }

    @Test
    public void testStart() {

        // 启动流程实例
        Map<String, Object> variables = new HashMap<>();
        variables.put("leaderAssignee",  LEADER_ZHANG);
        variables.put("hrAssignee",  HR_WANG);
        variables.put("ceoAssignee", CEO_LI);
        processExecController.startProcess(new ApprovalVo(APPROVE_USER, LEAVE_PROCESS, variables));
    }

    @Test
    public void testApproved() {
        // 1. 自动完成直属领导审批
        List<TaskInfoVo> res = processExecController.getPage(new TaskQo(LEADER_ZHANG, LEAVE_PROCESS, null));

        for (TaskInfoVo task : res) {
            Map<String, Object> variables = new HashMap<>();
            variables.put("comment", "同意请假");
            taskService.complete(task.getId(), variables);
        }
    }
    
    @Test
    public void testReject() {

    }


}